package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.CorpFaUserDTO;
import com.financing.wap.dto.FpFaUserSaysayCommentDTO;
import com.financing.wap.dto.FpFaUserSaysayDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpFaUserSaysayCommentService;
import com.financing.wap.service.IFpFaUserSaysayService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.PropertiesUtils;
import com.financing.wap.web.controller.BaseController;

@Controller("fpFaUserSaysayCommentController")
@RequestMapping("/api/fpFaUserSaysayComment")
public class FpFaUserSaysayCommentController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaUserSaysayCommentService fpFaUserSaysayCommentService;
	
	@Autowired
	private IFpFaUserSaysayService fpFaUserSaysayService;
	
	@Autowired
	private IUserService userService;
	
	/***
	 * 查询某说说下的所有评论
	 * @param request
	 * @param fpFaUserSaysayComment
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listFpFaUserSaysayComment")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserSaysayCommentDTO fpFaUserSaysayComment,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserSaysayComment list begin .......");
			
			fpFaUserSaysayComment.setBegin(pager.getStartIndex());
			fpFaUserSaysayComment.setPageSize(pager.getPageSize());
			
			Integer saysayId = fpFaUserSaysayComment.getSaysayId();
			if(saysayId==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			fpFaUserSaysayComment.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			int count = fpFaUserSaysayCommentService.queryListCount(fpFaUserSaysayComment);
			
			pager.setTotalCount(count);
			
			List<FpFaUserSaysayCommentDTO> fpFaUserSaysayCommentList = null;
			
			if(count > 0){
				fpFaUserSaysayCommentList = fpFaUserSaysayCommentService.queryList(fpFaUserSaysayComment);
				for (FpFaUserSaysayCommentDTO saysayCommentDto:fpFaUserSaysayCommentList) {
					String commentUserId = saysayCommentDto.getCommentUserId();
					if(commentUserId!=null){
						UserDTO u = userService.queryUserById(commentUserId);
						saysayCommentDto.setUserName(u.getUserName());
						saysayCommentDto.setUserHeadImg(u.getuImage());
					}else{
						saysayCommentDto.setUserName("游客");
						try{//去第一张默认头像
							String[] filesName = PropertiesUtils.getValue("faUserImageFile").split(";");
							saysayCommentDto.setUserHeadImg(PropertiesUtils.getValue("faUserImagePath")+filesName[0]);
						}catch(Exception e){
							Log.warn("create fa user image error",e);
						}
					}
				}
			}else{
				fpFaUserSaysayCommentList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaUserSaysayCommentList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserSaysayComment list end .......");
		}catch(Exception e){
			log.error("query fpFaUserSaysayComment error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 评论说说
	 * @param request
	 * @param fpFaUserSaysayComment
	 * @return
	 */
	@RequestMapping("/addFpFaUserSaysayComment")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserSaysayCommentDTO fpFaUserSaysayComment){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUserSaysayComment  begin .......");
			
			Integer saysayId = fpFaUserSaysayComment.getSaysayId();
			String saysayComment = fpFaUserSaysayComment.getSaysayComment();
			//评论id和评论内容没了
			if(saysayId==null||StringUtils.isBlank(saysayComment)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			UserDTO user = SessionUtil.getSession().getUser();
			if(user!=null&&user.getId()!=null){
				fpFaUserSaysayComment.setCommentUserId(user.getId());
			}
			
			fpFaUserSaysayComment.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			DTOdateSetUtil.setCreateInfo(fpFaUserSaysayComment);
			DTOdateSetUtil.setUpdateInfo(fpFaUserSaysayComment);
			
			int id = fpFaUserSaysayCommentService.insert(fpFaUserSaysayComment);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUserSaysayComment list end .......");
		}catch(Exception e){
			log.error("add fpFaUserSaysayComment error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/***
	 * 删除说说评论
	 * @param request
	 * @param fpFaUserSaysayComment
	 * @return
	 */
	@RequestMapping("/deleteFpFaUserSaysayComment")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserSaysayCommentDTO fpFaUserSaysayComment){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUserSaysayComment  begin .......");
			
			Integer id = fpFaUserSaysayComment.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserSaysayCommentDTO saysayComment = fpFaUserSaysayCommentService.loadById(id);
			if(saysayComment==null||saysayComment.getId()==null){
				result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
				result.setStatus(ResultMap.STATUS.SUCCESS);
				return result;
			}
			
			//是否有权限删除
			UserDTO user = SessionUtil.getSession().getUser();
			//当前登录的用户id
			String currentLoginUserId = user.getId();
			//说说id
			Integer saysayId = saysayComment.getSaysayId();
			FpFaUserSaysayDTO saysay = fpFaUserSaysayService.loadById(saysayId);
			String userId = saysay.getUserId();//发说说的userid
//			Integer commentUserId = saysayComment.getCommentUserId();//评论说说的userid
			//评论人&&发表说说用户
			//currentLoginUserId.equals(commentUserId)||
			boolean flagDelete=currentLoginUserId.equals(userId);
			if(flagDelete){
				fpFaUserSaysayCommentService.delete(id);
				result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
				result.setStatus(ResultMap.STATUS.SUCCESS);
			}else{
				//无权限
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				result.setStatus(ResultMap.STATUS.SUCCESS);
			}
			logger.info("delete fpFaUserSaysayComment  end .......");
		}catch(Exception e){
			log.error("delete fpFaUserSaysayComment error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
}
